package com.woniuxy.task;

import com.woniuxy.conmon.enums.RedisConst;
import com.woniuxy.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class ClearImageTask {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MinioUtils minioUtils;
    @Scheduled(cron = "0 0 0 * * ?")
    public void clearImage(){
        log.debug("清理无效图片定时任务开始");
        //获取redis两个集合的差集
        Set difference = redisTemplate.opsForSet().difference(RedisConst.UPLOAD_IMAGES, RedisConst.UPLOAD_IMAGES_DB);
        for (Object file : difference) {
            log.debug("无效的图片："+file);
            //调用minio工具类清除图片
            minioUtils.deleteImg(file.toString());
            //清除redis中的图片
            redisTemplate.opsForSet().remove(RedisConst.UPLOAD_IMAGES,file);
        }
        log.debug("清理无效图片定时任务结束");
    }
}
